#!/usr/bin/env python
#coding=utf-8
#system default setting
import PUBLICMETHOD
import sys
import os
import linecache

#system default setting
reload(sys)
sys.setdefaultencoding('utf-8')


#Tag
#########################################################################################################
#store the data of each tag
class Tag:

	def __init__(self, tagId, tagName, tagTypeId, tagFullId, tagOntologyId):
		self.__id = tagId
		self.__name = tagName
		self.__tagTypeId = tagTypeId
		self.__fullId = tagFullId
		self.__ontologyId = tagOntologyId

	def getTagId(self):
		return self.__id

	def getTagName(self):
		return self.__name

	def getTagTypeId(self):
		return self.__tagTypeId

	def getTagFullId(self):
		return self.__fullId

	def getOntologyId(self):
		return self.__ontologyId

class Ontology:

	def __init__(self):
		self.__ontologyId = self.__ontologyName = ""

	def getOntologyId(self):
		return self.__ontologyId

	def getOntologyName(self):
		return self.__ontologyName

class ShowedTag:
	def __init__(self):
		self.__tagId = self.__tagName = self.__ontologyName = ""

	def getShowedTag(self, ontologyDic, tag):
		self.__tagId = tag.getTagId()
		self.__tagName = tag.getTagName()
		#print "^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^"
		#print self.__tagName
		dicIndex = str(tag.getOntologyId())
		#print dicIndex
		self.__ontologyName = ontologyDic[dicIndex]
		#print self.__ontologyName
		return self

	def getShowedTagId(self):
		return self.__tagId

	def getShowedTagName(self):
		return self.__tagName

	def getOntologyName(self):
		return self.__ontologyName

class TagInform:
	def __init__(self):
		self.__matchedTagList = []
		self.__showedTagList = []

	def constructAndGainMatchedTagList(self, tagIndexList, DATPath):

		"""
		for tagIndex in tagIndexList:
			print tagIndex.getTagName()
			print tagIndex.getTagIndex()
		"""

		#print DATPath
		for tagIndex in tagIndexList:
			tagContent = linecache.getline(DATPath, tagIndex.getTagIndex() + 1)
			
			#print "****************************"
			#print tagIndex.getTagIndex() + 1
			#print tagContent
			#print "****************************"

			#delete "\n" which is in the end of tagContent
			tagContent = tagContent[:-1]
			#print tagContent
			tagData = tagContent.split('\t')

			#print tagData[4]
			#print tagData

			"""
			i = 0
			while i < 5:
				print str(i) + "=" + tagData[i]
				i += 1
			"""

			try:
				tag = Tag(tagData[0], tagData[1], tagData[2], tagData[3], tagData[4])
				self.__matchedTagList.append(tag)
			except Exception, ex:
				print "the TAG DAT files is illegal"
		return self.__matchedTagList


	def constrcutShowedTagList(self, ontologyDic):
		"""
		i = 1
		while i <= 15:
			print ontologyDic[i]
			i += 1
		"""
		
		"""
		for tag in self.__matchedTagList:
			print tag.getOntologyId()
		"""
		try:
			#i = 0
			for tag in self.__matchedTagList:
				showedTag = ShowedTag()
				self.__showedTagList.append(showedTag.getShowedTag(ontologyDic, tag))
				#print id(temp)
				#print id(self.__showedTagList)
				#print self.__showedTagList[i].getShowedTagId()
				#print self.__showedTagList[i].getShowedTagName()
				#print self.__showedTagList[i].getOntologyName()
				#i += 1
			#for tag in self.__showedTagList:
				#print id(tag)
				#print tag.getShowedTagId()
				#print tag.getShowedTagName()
				#print tag.getOntologyName()
		except Exception, ex:
			#print ex
			print "The Ontology Id is illegal"

		"""
		for tag in self.__showedTagList:	
			print tag.getShowedTagId()
			print tag.getShowedTagName()
			print tag.getOntologyName()
		i += 1
		"""
		#print self.__showedTagList.__len__()
		return self.__showedTagList
		

		

class TagIndex:

	def __init__(self, tagIndex, tagName):
		self.__tagIndex = tagIndex
		self.__tagName = tagName

	def getTagIndex(self):
		return self.__tagIndex

	def getTagName(self):
		return self.__tagName

class TagIndexList:

	def __init__(self):
		self.__index = self.__content = ""
		self.__countTag = 0
		self.__tagIndexList = []
		self.__matchTag = []

	def addTag(self, tagName):		
		tagIndex = TagIndex(self.__countTag, tagName)
		self.__tagIndexList.append(tagIndex)
		self.__countTag += 1

	def readDAT(self, DATPath):
		tagDataList = PUBLICMETHOD.ReadFile.readDAT(DATPath)
		isFirstLine = True
		for tagData in tagDataList:
			tagName = tagData.split('\t')
			self.addTag(tagName[1])
		#print id(tagDataList)
		del tagDataList
		PUBLICMETHOD.ReadFile.closeFile()

	def getTagIndexList(self):
		return self.__tagIndexList






#########################################################################################################